package com.hikvision.cms.common.util;

import com.hikvision.cms.dto.PassVehicleDto;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;


public class HttpUtils {
    public static String sendGet(String url, Map<String, String> parameters) {
        /*  30 */
        String result = "";
        /*  31 */
        BufferedReader in = null;
        /*  32 */
        StringBuffer sb = new StringBuffer();
        /*  33 */
        String params = "";

        try {
            /*  36 */
            if (parameters.size() == 1) {
                /*  37 */
                for (String name : parameters.keySet()) {
                    /*  38 */
                    sb.append(name).append("=").append(
                            /*  39 */               URLEncoder.encode(parameters.get(name), "UTF-8"));
                }

                /*  42 */
                params = sb.toString();
            } else {
                /*  44 */
                for (String name : parameters.keySet()) {
                    /*  45 */
                    sb.append(name).append("=").append(URLEncoder.encode(parameters.get(name), "UTF-8")).append("&");
                }
                /*  49 */
                String temp_params = sb.toString();
                /*  50 */
                params = temp_params.substring(0, temp_params.length() - 1);
            }
            /*  52 */
            String full_url = url + "?" + params;
            /*  53 */
            System.out.println(full_url);

            /*  55 */
            URL connURL = new URL(full_url);


            /*  58 */
            HttpURLConnection httpConn = (HttpURLConnection) connURL.openConnection();

            /*  60 */
            httpConn.setRequestProperty("Accept", "*/*");
            /*  61 */
            httpConn.setRequestProperty("Connection", "Keep-Alive");
            /*  62 */
            httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");


            /*  65 */
            httpConn.connect();

            /*  67 */
            Map<String, List<String>> headers = httpConn.getHeaderFields();

            /*  69 */
            for (String key : headers.keySet()) {
                /*  70 */
                System.out.println(key + "\t：\t" + headers.get(key));
            }


            /*  74 */
            in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8"));

            String line;
            /*  77 */
            while ((line = in.readLine()) != null) {
                /*  78 */
                result = result + line;
            }
            /*  80 */
        } catch (Exception e) {
            /*  81 */
            e.printStackTrace();
        } finally {
            try {
                /*  84 */
                if (in != null) {
                    /*  85 */
                    in.close();
                }
                /*  87 */
            } catch (IOException ex) {
                /*  88 */
                ex.printStackTrace();
            }
        }
        /*  91 */
        return result;
    }


    public static String sendPost(String url, Map<String, Object> parameters) {
        /* 104 */
        String result = "";
        /* 105 */
        BufferedReader in = null;
        /* 106 */
        PrintWriter out = null;
        /* 107 */
        StringBuffer sb = new StringBuffer();
        /* 108 */
        String params = "";

        try {
            /* 111 */
            if (parameters.size() == 1) {
                /* 112 */
                for (String name : parameters.keySet()) {
                    /* 113 */
                    sb.append(name).append("=").append(
                            /* 114 */               URLEncoder.encode(parameters.get(name).toString(), "UTF-8"));
                }

                /* 117 */
                params = sb.toString();
            } else {
                /* 119 */
                for (String name : parameters.keySet()) {
                    /* 121 */
                    sb.append(name).append("=").append(URLEncoder.encode((parameters.get(name) == null) ? "" : parameters.get(name).toString(), "UTF-8")).append("&");
                }
                /* 125 */
                String temp_params = sb.toString();
                /* 126 */
                params = temp_params.substring(0, temp_params.length() - 1);
            }

            /* 129 */
            URL connURL = new URL(url);


            /* 132 */
            HttpURLConnection httpConn = (HttpURLConnection) connURL.openConnection();

            /* 134 */
            httpConn.setRequestProperty("Accept", "*/*");
            /* 135 */
            httpConn.setRequestProperty("Connection", "Keep-Alive");
            /* 136 */
            httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");


            /* 139 */
            httpConn.setDoInput(true);
            /* 140 */
            httpConn.setDoOutput(true);

            /* 142 */
            out = new PrintWriter(httpConn.getOutputStream());

            /* 144 */
            out.write(params);

            /* 146 */
            out.flush();


            /* 149 */
            in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8"));

            String line;
            /* 152 */
            while ((line = in.readLine()) != null) {
                /* 153 */
                result = result + line;
            }
            /* 155 */
        } catch (Exception e) {
            /* 156 */
            e.printStackTrace();
        } finally {
            try {
                /* 159 */
                if (out != null) {
                    /* 160 */
                    out.close();
                }
                /* 162 */
                if (in != null) {
                    /* 163 */
                    in.close();
                }
                /* 165 */
            } catch (IOException ex) {
                /* 166 */
                ex.printStackTrace();
            }
        }
        /* 169 */
        return result;
    }


//    public static void main(String[] args) throws Exception {
//        /* 178 */
//        String s = "{\"plateNo\":\"浙F0EA03\", \"plateType\":\"1\", \"passTime\":\"2020-10-28 23:15:02\", \"direct\":\"0\", \"berthCode\":\"\", \"uuid\":\"0EDE1E28C1754987E6A277B\", \"gateCode\":\"0EDE1E28C17549859AFF@1\", \"plateColor\":\"1\", \"parkType\":\"1\", \"carType\":\"1\", \"laneNo\":\"0EDE1E28C17549859AFF_1\", \"inUnid\":\"\", \"parkCode\":null}";
//        /* 179 */
//        PassVehicleDto passVehicleDto = (PassVehicleDto) JSON.parseObject(s, PassVehicleDto.class);
//        /* 180 */
//        Map<String, Object> map = MapUtil.Obj2Map(passVehicleDto);
//        /* 181 */
//        String result = sendPost("https://www.s-park.vip/Api/HikStop/hik_stop_pass", map);
//        /* 182 */
//        System.out.println(result);
//    }
}

